package com.yuyou.tas.lesson.service.impl;

import com.yuyou.tas.lesson.mapper.StudentMapper;
import com.yuyou.tas.lesson.service.ILessonClassService;
import com.yuyou.tas.lesson.service.ILessonClassStudentService;
import com.yuyou.tas.lesson.service.IStudentService;
import com.yuyou.tas.service.api.admin.service.CommNoAPI;
import com.yuyou.tas.service.api.lesson.dto.LessonClassStudentPageDTO;
import com.yuyou.tas.service.api.lesson.dto.StudentDetailDTO;
import com.yuyou.tas.service.api.lesson.dto.StudentPageDTO;
import com.yuyou.tas.service.api.lesson.entity.LessonClass;
import com.yuyou.tas.service.api.lesson.entity.LessonClassStudent;
import com.yuyou.tas.service.api.lesson.entity.Student;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.tool.NoUtil;
import com.yuyou.tas.util.tool.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class StudentServiceImpl implements IStudentService {

	@Autowired
	private StudentMapper studentMapper;
	
	@Autowired
	private ILessonClassService lessonClassService;
	
	@Autowired
	private ILessonClassStudentService lessonClassStudentService;
	
	@Autowired
	private CommNoAPI commNoAPI;
	
	@Override
	public int deleteByPrimaryKey(Long id) {
		return studentMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Student record) {
		record.setId(Sequence.getSequence().nextId());
		record.setCreateTime(new Date());
		return studentMapper.insert(record);
	}

	@Override
	public Student selectByPrimaryKey(Long id) {
		return studentMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(Student record) {
		record.setUpdateTime(new Date());
		return studentMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	@Transactional(rollbackFor=Exception.class,timeout=60)
	public ResBoolSimpleInfo<String> sycnStudentByOId(List<Student> list) {		
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		for(Student l:list){
			Student student=this.selectByOrgan7OId(l.getoId(), l.getAuthOrganizationId());
			if(student==null){
				this.insert(l);				
				//查找班级学生，同步
				lessonClassStudentService.sycnStudent(l.getId(),l.getoId(), l.getAuthOrganizationId());	
			}else{
				student.setBaseSectionId(l.getBaseSectionId());
				student.setName(l.getName());
				student.setPhoneNo(l.getPhoneNo());
				student.setSex(l.getSex());
				student.setStudentNo(l.getStudentNo());
				student.setSlavePhoneNo(l.getSlavePhoneNo());
				this.updateByPrimaryKeySelective(student);				
				//查找班级学生，同步
				lessonClassStudentService.sycnStudent(student.getId(),student.getoId(), student.getAuthOrganizationId());	
			}
		
		}	
		res.setSuccess(true);
		return res;
	}

	@Override
	public Student selectByOrgan7OId(Long oId,Long authOrganizationId) {
		return studentMapper.selectByOrgan7OId(oId, authOrganizationId);
	}

	@Override
	public List<Student> selectByOrganId(Long authOrganizationId) {
		return studentMapper.selectByOrganId(authOrganizationId);
	}

	@Override
	public Student selectStudentByOrganIdStuNo(Long organId, String stuNo) {
		return studentMapper.selectStudentByOrganIdStuNo(organId , stuNo);
	}

	@Override
	public Student selectByStudentNo(String studentNo) {
		return studentMapper.selectByStudentNo(studentNo);
	}

	@Override
	public int findLessonClassStudentCount(
			Pagination<LessonClassStudentPageDTO> queryPgn) {
		return studentMapper.findLessonClassStudentCount(queryPgn);
	}

	@Override
	public List<LessonClassStudentPageDTO> findLessonClassStudentPageList(
			Pagination<LessonClassStudentPageDTO> queryPgn) {
		return studentMapper.findLessonClassStudentPageList(queryPgn);
	}

	@Override
	public List<Student> findNeedSycnClassStudent(String startTime) {
		return studentMapper.findNeedSycnClassStudent(startTime);
	}

	@Override
	public ResBoolSimpleInfo<String> addStudent(Student student) {
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		//同个姓名和手机号不能保存
		Student s=selectByOrganIdPhoneName(student.getAuthOrganizationId() , student.getPhoneNo() , student.getName());
		if(s != null){
			res.setMessage("已存在同个姓名和手机号学生，请检查是否有误");
			res.setSuccess(false);
			return res;
		}
		
		int noInt=commNoAPI.generNo(1);
//		String str = String.format("%06d", noInt); 
		student.setStudentNo(NoUtil.STUDENTNO+noInt);		
		this.insert(student);
		return ResBoolSimpleInfo.getSuccessInfo();
	}
	
	
	@Override
	public ResBoolSimpleInfo<String> updateStudent(Student student) {		
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		Student oriStudent=this.selectByPrimaryKey(student.getId());
		if(oriStudent.getoId()!=null){
			res.setMessage("不能修改");
			res.setSuccess(false);
			return res;
		}
		if(!student.getStudentNo().equals(oriStudent.getStudentNo())){
			Student judgeNo=this.selectByStudentNo(student.getStudentNo());
			if(judgeNo!=null){
				res.setMessage("学生编号已存在");
				res.setSuccess(false);
				return res;
			}
		}	
		List<Student> sList=selectByName7Phone(student.getName(),student.getPhoneNo());
		for(Student s:sList){
			if(s.getId().intValue()!=oriStudent.getId().intValue()){
				res.setMessage("已存在同个姓名和手机号学生，请检查是否有误");
				res.setSuccess(false);
				return res;
			}
		}
		this.updateByPrimaryKeySelective(student);	
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	@Transactional(rollbackFor=Exception.class,timeout=60)
	public ResBoolSimpleInfo<String> bindStudent(Long classId,
			List<Long> studentIds, Long authOrganizationId,Integer regStatus) {
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		LessonClass lessonClass=lessonClassService.selectByPrimaryKey(classId);
		if(lessonClass.getoId()!=null){
			res.setMessage("不能修改");
			res.setSuccess(false);
			return res;
		}		
		
		//不能添加业务平台的学生
//		long c=studentIds.stream().filter(item ->{			
//			Student s=this.selectByPrimaryKey(item);
//			if(s.getoId()!=null){
//				return true;
//			}
//			return false;
//		}).count();
//		if(c>0){
//			res.setMessage("不能添加正式学员");
//			res.setSuccess(false);
//			return res;
//		}	
		
		
		//删除班级学生regStatus的关系	
		lessonClassStudentService.deleteByClassId7regStatus(classId, regStatus,authOrganizationId);
				
		//新增班级学生regStatus的关系
		studentIds.forEach(item -> {
			LessonClassStudent lcs=new LessonClassStudent();
			lcs.setAuthOrganizationId(authOrganizationId);
			lcs.setLessonClassId(classId);
			lcs.setStudentId(item);
			lcs.setRegStatus(regStatus);
			lessonClassStudentService.insert(lcs);	
		});

		
		return ResBoolSimpleInfo.getSuccessInfo();
	}


	@Override
	public int findStudentCount(Pagination<StudentPageDTO> queryPgn) {
		return studentMapper.findStudentCount(queryPgn);
	}

	@Override
	public List<StudentPageDTO> findStudentPageList(
			Pagination<StudentPageDTO> queryPgn) {
		return studentMapper.findStudentPageList(queryPgn);
	}

	@Override
	public StudentDetailDTO detailStudent(Long id,
			Long authOrganizationId) {
		return studentMapper.selectByOrgan7Id(id, authOrganizationId);
	}

	@Override
	public List<Student> selectByName7Phone(String name, String phoneNo) {
		return studentMapper.selectByName7Phone(name, phoneNo);
	}

	@Override
	public Student selectByOrganIdPhoneName(Long authOrganizationId, String phoneNo, String name) {
		return studentMapper.selectByOrganIdPhoneName(authOrganizationId, phoneNo, name);
	}

	@Override
	public List<Student> findByOId(Long oId) {
		return studentMapper.findByOId(oId);
	}

}